package com.cheetah.start.module.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.cheetah.start.common.annotation.Log;
import com.cheetah.start.common.base.result.Result;
import com.cheetah.start.common.base.result.ResultController;
import com.cheetah.start.common.enums.BusinessTypeEnum;
import com.cheetah.start.common.enums.LogTypeEnum;
import com.cheetah.start.module.system.domain.SysDictType;
import com.cheetah.start.module.system.service.SysDictTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

/**
 * 字典类型控制器
 *
 * @author Feel
 * @date 2021/07/25
 */
@Api(tags = "字典类型")
@Slf4j
@Validated
@RestController
@RequestMapping("/system/dictType")
public class SysDictTypeController extends ResultController {
    @Autowired
    private SysDictTypeService dicTypeService;


    /**
     * 编辑
     *
     * @param dictTypeId dict类型id
     * @return {@link ModelAndView}
     */
    @ApiOperation(value = "编辑字典类型页面")
    @GetMapping("/edit")
    public ModelAndView edit(String dictTypeId) {
        SysDictType dictType = dicTypeService.getById(dictTypeId);
        ModelAndView model = new ModelAndView();
        model.addObject("sysDictType", dictType);
        model.setViewName("/system/dict/edit");
        return model;
    }


    /**
     * 启用
     *
     * @param sysDictType sys dict 类型
     * @return {@link Result}
     */
    @SaCheckPermission("sys:dictType:switch")
    @ApiOperation(value = "启用字典类型")
    @PostMapping("/enable")
    @Log(title = "字典类型", describe = "启用", type = BusinessTypeEnum.EDIT, logType = LogTypeEnum.OPERATE)
    public Result enable(@RequestBody SysDictType sysDictType) {
        SysDictType byId = dicTypeService.getById(sysDictType.getDictTypeId());
        byId.setEnable("0");
        return decide(dicTypeService.updateById(byId), BusinessTypeEnum.EDIT);
    }


    /**
     * 禁用
     *
     * @param sysDictType sys dict 类型
     * @return {@link Result}
     */
    @SaCheckPermission("sys:dictType:switch")
    @ApiOperation(value = "禁用字典类型")
    @PostMapping("/disable")
    @Log(title = "字典类型", describe = "禁用", type = BusinessTypeEnum.EDIT, logType = LogTypeEnum.OPERATE)
    public Result disable(@RequestBody SysDictType sysDictType) {
        SysDictType byId = dicTypeService.getById(sysDictType.getDictTypeId());
        byId.setEnable("1");
        return decide(dicTypeService.updateById(byId), BusinessTypeEnum.EDIT);
    }

}
